package com.qf.shiro2202day02.user.controller;

import com.qf.shiro2202day02.user.entity.User;
import com.qf.shiro2202day02.user.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    @PostMapping("/login")
    public String login(String username,String password,boolean rememberMe){

        //1. 获取shiro的核心对象 subject
        final Subject subject = SecurityUtils.getSubject();

        // 第三个参数，代表是否开启免登录功能
        subject.login(new UsernamePasswordToken(username,password,rememberMe));

        return "redirect:/home.html";
    }

    @GetMapping("/logout")
    public String logout(){

        final Subject subject = SecurityUtils.getSubject();

        subject.logout();  // 注销当前用户

        return "redirect:/login.html";

    }


    @PostMapping("/register")
    public String regist(User user){

        userService.regist(user);

        return "redirect:/login.html";
    }
}
